package Controller;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.Serializable;
import Communication.Client;
import Communication.MessageHandle;
import View.LoginView;
import Controller.SystemController;
import Entity.Worker;

/** This class controls and manages the process that allows the users to enter the
 * the system by entering their user name and password. 
 */
public class LoginController  implements Serializable {
	private static final long serialVersionUID = 42L;
	/**  Required attributes */
	LoginView login;
	Client client;
	Worker worker ;

	/**
	 * Constructor calling to listener class that attached to the process
	 * @param client
	 */
	public LoginController(Client client) {
		login = new LoginView();
		this.client = client;
		login.addLoginListeners(new LoginListener());
	}

	/**
	 *  shows window
	 */
	public void showWindow() {
		login.showWindow();
	}

	/**
	 *  hidea window
	 */
	public void hideWindow() {
		login.hideWindow();
	}


	/** Inner class to implement button clicks*/
	class LoginListener implements ActionListener {


		WorkerMainMenuController workerMainMenuController;

		int status;
		int role;
		String workerId;

		public LoginListener() {	
			workerMainMenuController = new WorkerMainMenuController(client,
					LoginController.this);
		}

		/**
		 * Handle Actions
		 */
		public void actionPerformed(ActionEvent event) {


			if (event.getSource() == login.getLoginButton()) {


				if (login.getUsername().isEmpty() || login.getPassword().isEmpty())
					SystemController
					.displayErrorMessage("One or both of the fields are empty");


				else  {

					worker=new Worker ();
					worker.getWorkerFromDB(client, login.getUsername());
					try {
						Thread.sleep(2000);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

					/**
					 *Open workers main window with buttons according to permission
					 */
					if (client.getCurrentWorker().getWorkerID() == null )
						SystemController
						.displayErrorMessage("User doesnt exist in database");
					else {
						status = client.getCurrentWorker().getStatus();
						role = client.getCurrentWorker().getRole();
						System.out.println(client.getCurrentWorker().getWorkerID());
						workerId = client.getCurrentWorker().getWorkerID();
						client.setCurrentWorkerID(workerId);

						// Password is correct
						if (login.getPassword().equals(client.getCurrentWorker().getPassword())) {
							switch (status) {

							case 1: 
								SystemController.displayErrorMessage("Worker already logged-in !");
								break;
							case 0:		worker.setStatusDB(client,1);			

							login.hideWindow();


							boolean[] availableButtons = new boolean[9];
							workerMainMenuController.enableAll();
							for(int i=0;i<availableButtons.length;i++)										
							{
								availableButtons[i] = false;										
							}								

							/**Set the active buttons according to workers permission*/
							switch (role) {

							/**
							 * in case the role is 4= marketing manager 
							 */
							case 4:  						
								availableButtons[1] = true; //NewPattern								
								availableButtons[2] = true;	//ProductToCatalog									
								availableButtons[3] = true;	//RunSale							
								availableButtons[5] = true; //Reports
								availableButtons[6] = true;	//handle catalog
								availableButtons[8] = true; //HandleCity
								MessageHandle msg4 = new MessageHandle(null, MessageHandle.GET_CITIES, false);
								try {
									client.sendToServer(msg4);
								} catch (IOException e) {
									System.out.println("Relations person->case 4");
									e.printStackTrace();
								}

								break;

								/**
								 * in case the role is 3= marketing man
								 */
							case 3:	 								
								availableButtons[1] = true;	//NewPattern									
								availableButtons[2] = true;	//ProductToCatalog								
								MessageHandle msg3 = new MessageHandle(null, MessageHandle.GET_CITIES, false);
								try {
									client.sendToServer(msg3);
								} catch (IOException e) {
									System.out.println("Relations person->case 3");
									e.printStackTrace();
								}
								break;
								/**
								 * in case the role is 2= sales man
								 */
							case 2:	
								availableButtons[0] = true;	//EnterNewCustomer	
								availableButtons[4] = true;	//HandleSale
								availableButtons[7] = true; //UpdateCustomer
								MessageHandle msg2 = new MessageHandle(null, MessageHandle.GET_CITIES, false);
								try {
									client.sendToServer(msg2);
								} catch (IOException e) {
									System.out.println("Relations person->case 2");
									e.printStackTrace();
								}
								break;
								/**
								 * in case the role is 1= Relations person
								 */
							case 1:	// Relations person
								availableButtons[0] = true; //EnterNewCustomer
								availableButtons[7] = true; //UpdateCustomer
								MessageHandle msg = new MessageHandle(null, MessageHandle.GET_CITIES, false);
								try {
									client.sendToServer(msg);
								} catch (IOException e) {
									System.out.println("Relations person->case 1");
									e.printStackTrace();
								}
								break;

							}




							workerMainMenuController.setActiveButtons(availableButtons);

							// Show main worker window
							workerMainMenuController.showWindow();
							hideWindow();
							break;
							}

						}


						// Password is incorrect
						else {
							// Decrease and set new number of attempts left to
							// DB
							SystemController.displayErrorMessage("Username or password incorrect !");


						}


					}

				}
			}
			else{
				if(event.getSource() == login.getCancelButton()){
					login.dispose();
					System.exit(0);
				}

			}
			// Clear fields
			login.clearPassword();
			login.clearUsername();




		}
	}
}

